﻿using System;
using System.Diagnostics;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Searches;

namespace Tests
{
    [TestClass]
    public class Test
    {
        [TestMethod]
        public void CompareAlgorythms()
        {
            for (int i = 1; i <= 4; i++)
            {
                PerformTest(5*(int) Math.Pow(10, i));
            }
        }

        private void PerformTest(int numberOfElements)
        {
            Console.WriteLine("Performing test on " + numberOfElements + " elements");

            int[] array = Enumerable.Range(1, numberOfElements).OrderBy(x => Guid.NewGuid()).ToArray();
            int[] arrayForInsertion = array.ToArray();
            int[] arrayForBubble = array.ToArray();
            int[] arrayForMerge = array.ToArray();
            int[] arrayForQuick = array.ToArray();

            Stopwatch stopwatch = Stopwatch.StartNew();
            int[] sortedByLinq = array.OrderBy(x => x).ToArray();
            Console.WriteLine("{0,30}: {1}", "Linq time", stopwatch.Elapsed);

            stopwatch.Restart();
            arrayForInsertion.InsertionSort();
            Console.WriteLine("{0,30}: {1}", "Insertion time", stopwatch.Elapsed);
            CollectionAssert.AreEqual(sortedByLinq, arrayForInsertion);

            stopwatch.Restart();
            arrayForBubble.BubbleSearch();
            Console.WriteLine("{0,30}: {1}", "Bubble time", stopwatch.Elapsed);
            CollectionAssert.AreEqual(sortedByLinq, arrayForBubble);

            stopwatch.Restart();
            arrayForMerge.MergeSort();
            Console.WriteLine("{0,30}: {1}", "Merge time", stopwatch.Elapsed);
            CollectionAssert.AreEqual(sortedByLinq, arrayForMerge);

            stopwatch.Restart();
            arrayForQuick.QuickSort();
            Console.WriteLine("{0,30}: {1}", "Quicksort time", stopwatch.Elapsed);
            CollectionAssert.AreEqual(sortedByLinq, arrayForQuick);

            Console.WriteLine("**********************************************");
        }
    }
}
